package com.yang.common.core.retry;

/**
 * @author: Billyang
 * @date: 2024/9/8 17:24
 */
public class RetryDemo {
    public static void main(String[] args) {
        RetryTemplate retryTemplate = RetryTemplate.builder()
                .retryTimes(3)
                .delayMillis(1000)
                .maxDelayMillis(5000).build();
        try{
            String result = retryTemplate.execute(() -> {
                double random = Math.random();
                System.out.println("随机值：" + random);
                if (random > 0.5) {
                    throw new RuntimeException("操作失败");
                }
                return "操作成功";
            });
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("代码执行失败");
        }

    }
}
